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