2003-06-12 H.J. Lu <hongjiu.lu@intel.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
0f20eeea 60 - And operator names can contain parentheses or angle brackets. */
9219021c
DC
61
62/* FIXME: carlton/2003-03-13: We have several functions here with
63 overlapping functionality; can we combine them? Also, do they
64 handle all the above considerations correctly? */
de17c821
DJ
65
66/* Find the last component of the demangled C++ name NAME. NAME
67 must be a method name including arguments, in order to correctly
68 locate the last component.
69
70 This function return a pointer to the first colon before the
71 last component, or NULL if the name had only one component. */
72
73static const char *
74find_last_component (const char *name)
75{
76 const char *p;
77 int depth;
78
79 /* Functions can have local classes, so we need to find the
80 beginning of the last argument list, not the end of the first
81 one. */
82 p = name + strlen (name) - 1;
83 while (p > name && *p != ')')
84 p--;
85
86 if (p == name)
87 return NULL;
88
89 /* P now points at the `)' at the end of the argument list. Walk
90 back to the beginning. */
91 p--;
92 depth = 1;
93 while (p > name && depth > 0)
94 {
95 if (*p == '<' || *p == '(')
96 depth--;
97 else if (*p == '>' || *p == ')')
98 depth++;
99 p--;
100 }
101
102 if (p == name)
103 return NULL;
104
105 while (p > name && *p != ':')
106 p--;
107
108 if (p == name || p == name + 1 || p[-1] != ':')
109 return NULL;
110
111 return p - 1;
112}
113
114/* Return the name of the class containing method PHYSNAME. */
115
116char *
117class_name_from_physname (const char *physname)
118{
119 char *ret = NULL;
120 const char *end;
121 int depth = 0;
122 char *demangled_name = cplus_demangle (physname, DMGL_ANSI);
123
124 if (demangled_name == NULL)
125 return NULL;
126
127 end = find_last_component (demangled_name);
128 if (end != NULL)
129 {
130 ret = xmalloc (end - demangled_name + 1);
131 memcpy (ret, demangled_name, end - demangled_name);
132 ret[end - demangled_name] = '\0';
133 }
134
135 xfree (demangled_name);
136 return ret;
137}
138
139/* Return the name of the method whose linkage name is PHYSNAME. */
140
141char *
142method_name_from_physname (const char *physname)
143{
144 char *ret = NULL;
145 const char *end;
146 int depth = 0;
147 char *demangled_name = cplus_demangle (physname, DMGL_ANSI);
148
149 if (demangled_name == NULL)
150 return NULL;
151
152 end = find_last_component (demangled_name);
153 if (end != NULL)
154 {
155 char *args;
156 int len;
157
158 /* Skip "::". */
159 end = end + 2;
160
161 /* Find the argument list, if any. */
162 args = strchr (end, '(');
163 if (args == NULL)
164 len = strlen (end + 2);
165 else
166 {
167 args --;
168 while (*args == ' ')
169 args --;
170 len = args - end + 1;
171 }
172 ret = xmalloc (len + 1);
173 memcpy (ret, end, len);
174 ret[len] = 0;
175 }
176
177 xfree (demangled_name);
178 return ret;
179}
9219021c
DC
180
181/* This returns the length of first component of NAME, which should be
182 the demangled name of a C++ variable/function/method/etc.
183 Specifically, it returns the index of the first colon forming the
184 boundary of the first component: so, given 'A::foo' or 'A::B::foo'
185 it returns the 1, and given 'foo', it returns 0. */
186
187/* Well, that's what it should do when called externally, but to make
188 the recursion easier, it also stops if it reaches an unexpected ')'
189 or '>'. */
190
191/* NOTE: carlton/2003-03-13: This function is currently only intended
192 for internal use: it's probably not entirely safe when called on
193 user-generated input, because some of the 'index += 2' lines might
194 go past the end of malformed input. */
195
196/* Let's optimize away calls to strlen("operator"). */
197
198#define LENGTH_OF_OPERATOR 8
199
200unsigned int
201cp_find_first_component (const char *name)
202{
9219021c 203 unsigned int index = 0;
0f20eeea
DC
204 /* Operator names can show up in unexpected places. Since these can
205 contain parentheses or angle brackets, they can screw up the
206 recursion. But not every string 'operator' is part of an
207 operater name: e.g. you could have a variable 'cooperator'. So
208 this variable tells us whether or not we should treat the string
209 'operator' as starting an operator. */
210 int operator_possible = 1;
9219021c
DC
211
212 for (;; ++index)
213 {
214 switch (name[index])
215 {
216 case '<':
217 /* Template; eat it up. The calls to cp_first_component
218 should only return (I hope!) when they reach the '>'
219 terminating the component or a '::' between two
220 components. (Hence the '+ 2'.) */
221 index += 1;
222 for (index += cp_find_first_component (name + index);
223 name[index] != '>';
224 index += cp_find_first_component (name + index))
225 {
226 gdb_assert (name[index] == ':');
227 index += 2;
228 }
0f20eeea 229 operator_possible = 1;
9219021c
DC
230 break;
231 case '(':
232 /* Similar comment as to '<'. */
233 index += 1;
234 for (index += cp_find_first_component (name + index);
235 name[index] != ')';
236 index += cp_find_first_component (name + index))
237 {
238 gdb_assert (name[index] == ':');
239 index += 2;
240 }
0f20eeea 241 operator_possible = 1;
9219021c
DC
242 break;
243 case '>':
244 case ')':
245 case '\0':
246 case ':':
247 return index;
0f20eeea
DC
248 case 'o':
249 /* Operator names can screw up the recursion. */
250 if (operator_possible
251 && strncmp (name + index, "operator", LENGTH_OF_OPERATOR) == 0)
252 {
253 index += LENGTH_OF_OPERATOR;
254 while (isspace(name[index]))
255 ++index;
256 switch (name[index])
257 {
258 /* Skip over one less than the appropriate number of
259 characters: the for loop will skip over the last
260 one. */
261 case '<':
262 if (name[index + 1] == '<')
263 index += 1;
264 else
265 index += 0;
266 break;
267 case '>':
268 case '-':
269 if (name[index + 1] == '>')
270 index += 1;
271 else
272 index += 0;
273 break;
274 case '(':
275 index += 1;
276 break;
277 default:
278 index += 0;
279 break;
280 }
281 }
282 operator_possible = 0;
283 break;
284 case ' ':
285 case ',':
286 case '.':
287 case '&':
288 case '*':
289 /* NOTE: carlton/2003-04-18: I'm not sure what the precise
290 set of relevant characters are here: it's necessary to
291 include any character that can show up before 'operator'
292 in a demangled name, and it's safe to include any
293 character that can't be part of an identifier's name. */
294 operator_possible = 1;
295 break;
9219021c 296 default:
0f20eeea 297 operator_possible = 0;
9219021c
DC
298 break;
299 }
300 }
301}
302
303/* If NAME is the fully-qualified name of a C++
304 function/variable/method/etc., this returns the length of its
305 entire prefix: all of the namespaces and classes that make up its
306 name. Given 'A::foo', it returns 1, given 'A::B::foo', it returns
307 4, given 'foo', it returns 0. */
308
309unsigned int
310cp_entire_prefix_len (const char *name)
311{
312 unsigned int current_len = cp_find_first_component (name);
313 unsigned int previous_len = 0;
314
315 while (name[current_len] != '\0')
316 {
317 gdb_assert (name[current_len] == ':');
318 previous_len = current_len;
319 /* Skip the '::'. */
320 current_len += 2;
321 current_len += cp_find_first_component (name + current_len);
322 }
323
324 return previous_len;
325}
326
327/* Don't allow just "maintenance cplus". */
328
329static void
330maint_cplus_command (char *arg, int from_tty)
331{
332 printf_unfiltered ("\"maintenance cplus\" must be followed by the name of a command.\n");
333 help_list (maint_cplus_cmd_list, "maintenance cplus ", -1, gdb_stdout);
334}
335
336/* This is a front end for cp_find_first_component, for unit testing.
337 Be careful when using it: see the NOTE above
338 cp_find_first_component. */
339
340static void
341first_component_command (char *arg, int from_tty)
342{
343 int len = cp_find_first_component (arg);
344 char *prefix = alloca (len + 1);
345
346 memcpy (prefix, arg, len);
347 prefix[len] = '\0';
348
349 printf_unfiltered ("%s\n", prefix);
350}
351
b9362cc7
AC
352extern initialize_file_ftype _initialize_cp_support; /* -Wmissing-prototypes */
353
9219021c
DC
354void
355_initialize_cp_support (void)
356{
357 add_prefix_cmd ("cplus", class_maintenance, maint_cplus_command,
358 "C++ maintenance commands.", &maint_cplus_cmd_list,
359 "maintenance cplus ", 0, &maintenancelist);
360 add_alias_cmd ("cp", "cplus", class_maintenance, 1, &maintenancelist);
361
362 add_cmd ("first_component", class_maintenance, first_component_command,
363 "Print the first class/namespace component of NAME.",
364 &maint_cplus_cmd_list);
365
366}
This page took 0.106749 seconds and 4 git commands to generate.