2003-04-23 Andrew Cagney <cagney@redhat.com>
[deliverable/binutils-gdb.git] / gdb / cp-support.c
CommitLineData
de17c821 1/* Helper routines for C++ support in GDB.
9219021c 2 Copyright 2002, 2003 Free Software Foundation, Inc.
de17c821
DJ
3
4 Contributed by MontaVista Software.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
22
23#include "defs.h"
9219021c 24#include <ctype.h>
de17c821
DJ
25#include "cp-support.h"
26#include "gdb_string.h"
27#include "demangle.h"
9219021c
DC
28#include "gdb_assert.h"
29#include "gdbcmd.h"
30
31/* The list of "maint cplus" commands. */
32
33static struct cmd_list_element *maint_cplus_cmd_list = NULL;
34
35/* The actual commands. */
36
37static void maint_cplus_command (char *arg, int from_tty);
38static void first_component_command (char *arg, int from_tty);
39
40/* Here are some random pieces of trivia to keep in mind while trying
41 to take apart demangled names:
42
43 - Names can contain function arguments or templates, so the process
44 has to be, to some extent recursive: maybe keep track of your
45 depth based on encountering <> and ().
46
47 - Parentheses don't just have to happen at the end of a name: they
48 can occur even if the name in question isn't a function, because
49 a template argument might be a type that's a function.
50
51 - Conversely, even if you're trying to deal with a function, its
52 demangled name might not end with ')': it could be a const or
53 volatile class method, in which case it ends with "const" or
54 "volatile".
55
56 - Parentheses are also used in anonymous namespaces: a variable
57 'foo' in an anonymous namespace gets demangled as "(anonymous
58 namespace)::foo".
59
60 - And operator names can contain parentheses or angle brackets.
61 Fortunately, I _think_ that operator names can only occur in a
62 fairly restrictive set of locations (in particular, they have be
63 at depth 0, don't they?). */
64
65/* NOTE: carlton/2003-02-21: Daniel Jacobowitz came up with an example
66 where operator names don't occur at depth 0. Sigh. (It involved a
67 template argument that was a pointer: I hadn't realized that was
68 possible.) Handling such edge cases does not seem like a
69 high-priority problem to me. */
70
71/* FIXME: carlton/2003-03-13: We have several functions here with
72 overlapping functionality; can we combine them? Also, do they
73 handle all the above considerations correctly? */
de17c821
DJ
74
75/* Find the last component of the demangled C++ name NAME. NAME
76 must be a method name including arguments, in order to correctly
77 locate the last component.
78
79 This function return a pointer to the first colon before the
80 last component, or NULL if the name had only one component. */
81
82static const char *
83find_last_component (const char *name)
84{
85 const char *p;
86 int depth;
87
88 /* Functions can have local classes, so we need to find the
89 beginning of the last argument list, not the end of the first
90 one. */
91 p = name + strlen (name) - 1;
92 while (p > name && *p != ')')
93 p--;
94
95 if (p == name)
96 return NULL;
97
98 /* P now points at the `)' at the end of the argument list. Walk
99 back to the beginning. */
100 p--;
101 depth = 1;
102 while (p > name && depth > 0)
103 {
104 if (*p == '<' || *p == '(')
105 depth--;
106 else if (*p == '>' || *p == ')')
107 depth++;
108 p--;
109 }
110
111 if (p == name)
112 return NULL;
113
114 while (p > name && *p != ':')
115 p--;
116
117 if (p == name || p == name + 1 || p[-1] != ':')
118 return NULL;
119
120 return p - 1;
121}
122
123/* Return the name of the class containing method PHYSNAME. */
124
125char *
126class_name_from_physname (const char *physname)
127{
128 char *ret = NULL;
129 const char *end;
130 int depth = 0;
131 char *demangled_name = cplus_demangle (physname, DMGL_ANSI);
132
133 if (demangled_name == NULL)
134 return NULL;
135
136 end = find_last_component (demangled_name);
137 if (end != NULL)
138 {
139 ret = xmalloc (end - demangled_name + 1);
140 memcpy (ret, demangled_name, end - demangled_name);
141 ret[end - demangled_name] = '\0';
142 }
143
144 xfree (demangled_name);
145 return ret;
146}
147
148/* Return the name of the method whose linkage name is PHYSNAME. */
149
150char *
151method_name_from_physname (const char *physname)
152{
153 char *ret = NULL;
154 const char *end;
155 int depth = 0;
156 char *demangled_name = cplus_demangle (physname, DMGL_ANSI);
157
158 if (demangled_name == NULL)
159 return NULL;
160
161 end = find_last_component (demangled_name);
162 if (end != NULL)
163 {
164 char *args;
165 int len;
166
167 /* Skip "::". */
168 end = end + 2;
169
170 /* Find the argument list, if any. */
171 args = strchr (end, '(');
172 if (args == NULL)
173 len = strlen (end + 2);
174 else
175 {
176 args --;
177 while (*args == ' ')
178 args --;
179 len = args - end + 1;
180 }
181 ret = xmalloc (len + 1);
182 memcpy (ret, end, len);
183 ret[len] = 0;
184 }
185
186 xfree (demangled_name);
187 return ret;
188}
9219021c
DC
189
190/* This returns the length of first component of NAME, which should be
191 the demangled name of a C++ variable/function/method/etc.
192 Specifically, it returns the index of the first colon forming the
193 boundary of the first component: so, given 'A::foo' or 'A::B::foo'
194 it returns the 1, and given 'foo', it returns 0. */
195
196/* Well, that's what it should do when called externally, but to make
197 the recursion easier, it also stops if it reaches an unexpected ')'
198 or '>'. */
199
200/* NOTE: carlton/2003-03-13: This function is currently only intended
201 for internal use: it's probably not entirely safe when called on
202 user-generated input, because some of the 'index += 2' lines might
203 go past the end of malformed input. */
204
205/* Let's optimize away calls to strlen("operator"). */
206
207#define LENGTH_OF_OPERATOR 8
208
209unsigned int
210cp_find_first_component (const char *name)
211{
212 /* Names like 'operator<<' screw up the recursion, so let's
213 special-case them. I _hope_ they can only occur at the start of
214 a component. */
215
216 unsigned int index = 0;
217
218 if (strncmp (name, "operator", LENGTH_OF_OPERATOR) == 0)
219 {
220 index += LENGTH_OF_OPERATOR;
221 while (isspace(name[index]))
222 ++index;
223 switch (name[index])
224 {
225 case '<':
226 if (name[index + 1] == '<')
227 index += 2;
228 else
229 index += 1;
230 break;
231 case '>':
232 case '-':
233 if (name[index + 1] == '>')
234 index += 2;
235 else
236 index += 1;
237 break;
238 case '(':
239 index += 2;
240 break;
241 default:
242 index += 1;
243 break;
244 }
245 }
246
247 for (;; ++index)
248 {
249 switch (name[index])
250 {
251 case '<':
252 /* Template; eat it up. The calls to cp_first_component
253 should only return (I hope!) when they reach the '>'
254 terminating the component or a '::' between two
255 components. (Hence the '+ 2'.) */
256 index += 1;
257 for (index += cp_find_first_component (name + index);
258 name[index] != '>';
259 index += cp_find_first_component (name + index))
260 {
261 gdb_assert (name[index] == ':');
262 index += 2;
263 }
264 break;
265 case '(':
266 /* Similar comment as to '<'. */
267 index += 1;
268 for (index += cp_find_first_component (name + index);
269 name[index] != ')';
270 index += cp_find_first_component (name + index))
271 {
272 gdb_assert (name[index] == ':');
273 index += 2;
274 }
275 break;
276 case '>':
277 case ')':
278 case '\0':
279 case ':':
280 return index;
281 default:
282 break;
283 }
284 }
285}
286
287/* If NAME is the fully-qualified name of a C++
288 function/variable/method/etc., this returns the length of its
289 entire prefix: all of the namespaces and classes that make up its
290 name. Given 'A::foo', it returns 1, given 'A::B::foo', it returns
291 4, given 'foo', it returns 0. */
292
293unsigned int
294cp_entire_prefix_len (const char *name)
295{
296 unsigned int current_len = cp_find_first_component (name);
297 unsigned int previous_len = 0;
298
299 while (name[current_len] != '\0')
300 {
301 gdb_assert (name[current_len] == ':');
302 previous_len = current_len;
303 /* Skip the '::'. */
304 current_len += 2;
305 current_len += cp_find_first_component (name + current_len);
306 }
307
308 return previous_len;
309}
310
311/* Don't allow just "maintenance cplus". */
312
313static void
314maint_cplus_command (char *arg, int from_tty)
315{
316 printf_unfiltered ("\"maintenance cplus\" must be followed by the name of a command.\n");
317 help_list (maint_cplus_cmd_list, "maintenance cplus ", -1, gdb_stdout);
318}
319
320/* This is a front end for cp_find_first_component, for unit testing.
321 Be careful when using it: see the NOTE above
322 cp_find_first_component. */
323
324static void
325first_component_command (char *arg, int from_tty)
326{
327 int len = cp_find_first_component (arg);
328 char *prefix = alloca (len + 1);
329
330 memcpy (prefix, arg, len);
331 prefix[len] = '\0';
332
333 printf_unfiltered ("%s\n", prefix);
334}
335
336void
337_initialize_cp_support (void)
338{
339 add_prefix_cmd ("cplus", class_maintenance, maint_cplus_command,
340 "C++ maintenance commands.", &maint_cplus_cmd_list,
341 "maintenance cplus ", 0, &maintenancelist);
342 add_alias_cmd ("cp", "cplus", class_maintenance, 1, &maintenancelist);
343
344 add_cmd ("first_component", class_maintenance, first_component_command,
345 "Print the first class/namespace component of NAME.",
346 &maint_cplus_cmd_list);
347
348}
This page took 0.078779 seconds and 4 git commands to generate.